package com.example.websocket.redis.queue;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.redisson.api.RBlockingDeque;
import org.redisson.api.RDelayedQueue;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * redis延迟队列服务
 * @author David
 *
 */
//@Component
public class RedisDelayJobService {

	private static final Logger LOGGER = LogManager.getLogger(RedisDelayJobService.class);
	
	@Autowired
	private RedissonClient redissonClient;
	
	/**
	 * 添加到消息队列
	 * @param value     添加到队列的对象
	 * @param delay     延迟时间
	 * @param timeUnit  时间单位
	 */
	public <T> void addQueue(T value, long delay, TimeUnit timeUnit, Class<? extends RedisDelayedQueueListener<T>> clazz) {
		LOGGER.info("-----> add queue, deplay: {}, queueName:{}", delay, clazz.getName());
		RBlockingDeque<Object> blockingDeque = redissonClient.getBlockingDeque(clazz.getName());
		RDelayedQueue<Object> delayedQueue = redissonClient.getDelayedQueue(blockingDeque);
		delayedQueue.offer(value, delay, timeUnit);
	}

}
